package com.sw.small_world.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sw.small_world.R;
import com.sw.small_world.adapter.MainPagerAdapter;
import com.sw.small_world.fragment.home.BaseHomeFragment;
import com.sw.small_world.utils.PopUtils;
import com.sw.small_world.utils.ShareUtils;
import com.sw.small_world.utils.StartUtils;
import com.sw.small_world.widgets.NoScrollViewPager;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    @Bind(R.id.viewpager)
    NoScrollViewPager viewpager;
    @Bind(R.id.bottom_home)
    RadioButton bottomHome;
    @Bind(R.id.bottom_small_world)
    RadioButton bottomSmallWorld;
    @Bind(R.id.bottom_message)
    RadioButton bottomMessage;
    @Bind(R.id.bottom_me)
    RadioButton bottomMe;
    @Bind(R.id.radio_group)
    RadioGroup radioGroup;
    @Bind(R.id.bottom_publish)
    ImageView bottomPublish;
    @Bind(R.id.ren_mai)
    public ImageView renMai;
    @Bind(R.id.action_bar_back)
    public ImageView actionBarBack;
    @Bind(R.id.tv_title)
    public TextView tvTitle;
    @Bind(R.id.iv_right)
    public ImageView ivRight;
    @Bind(R.id.tv_right)
    public TextView tvRight;
    @Bind(R.id.rl_actionbar)
    public RelativeLayout rlActionbar;
    @Bind(R.id.home_search)
    public LinearLayout homeSearch;
    @Bind(R.id.pop_view)
    public View popView;
    @Bind(R.id.view)
    View view;
    private MainPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        viewpager.setCurrentItem(0);
        bottomHome.setChecked(true);
        viewpager.setOffscreenPageLimit(10);
        adapter = new MainPagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(adapter);
        bottomPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new PopUtils().initPublishPop(MainActivity.this, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        StartUtils.startActivityById(MainActivity.this, v.getId());
                    }
                }).showAsDropDown(popView);
            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.bottom_home:
                        viewpager.setCurrentItem(0, false);
                        actionBarBack.setVisibility(View.VISIBLE);
                        ivRight.setVisibility(View.GONE);
                        renMai.setVisibility(View.GONE);
                        actionBarBack.setVisibility(View.VISIBLE);
                        int position = ShareUtils.getInt(ShareUtils.IS_HOME, 0);
                        if (position == 0) {
                            homeSearch.setVisibility(View.VISIBLE);
                        } else if (position == 1) {
                            rlActionbar.setVisibility(View.GONE);
                        } else if (position == 2) {
                            tvTitle.setText("话题广场");
                        } else if (position == 3) {
                            tvTitle.setText("人脉办事");
                        } else if (position == 4) {
                            tvTitle.setText("小道消息");
                        } else if (position == 5) {
                            tvTitle.setText("职场百科");
                        }
                        break;
                    case R.id.bottom_small_world:
                        viewpager.setCurrentItem(1, false);
                        ivRight.setVisibility(View.VISIBLE);
                        renMai.setVisibility(View.GONE);
                        actionBarBack.setVisibility(View.VISIBLE);
                        homeSearch.setVisibility(View.GONE);
                        rlActionbar.setVisibility(View.VISIBLE);
                        actionBarBack.setVisibility(View.GONE);
                        tvTitle.setText("小世界");
                        break;
                    case R.id.bottom_message:
                        ivRight.setVisibility(View.GONE);
                        viewpager.setCurrentItem(2, false);
                        actionBarBack.setVisibility(View.GONE);
                        homeSearch.setVisibility(View.GONE);
                        rlActionbar.setVisibility(View.VISIBLE);
                        renMai.setVisibility(View.VISIBLE);
                        actionBarBack.setVisibility(View.GONE);
                        tvTitle.setText("消息");
                        break;
                    case R.id.bottom_me:
                        renMai.setVisibility(View.GONE);
                        actionBarBack.setVisibility(View.VISIBLE);
                        ivRight.setVisibility(View.GONE);
                        viewpager.setCurrentItem(3, false);
                        actionBarBack.setVisibility(View.GONE);
                        homeSearch.setVisibility(View.GONE);
                        rlActionbar.setVisibility(View.VISIBLE);
                        tvTitle.setText("我的");
                        break;
                }
            }
        });
        actionBarBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
                homeSearch.setVisibility(View.VISIBLE);
                ShareUtils.putInt(ShareUtils.IS_HOME, 0);
            }
        });
    }

    public void setCurrentItem(int position) {
        BaseHomeFragment fragment = (BaseHomeFragment) adapter.getItem(0);
        fragment.setCurrentFragment(position);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && viewpager.getCurrentItem() != 0) {
            this.finish();
            return true;
        } else {
            if (keyCode == KeyEvent.KEYCODE_BACK && viewpager.getCurrentItem() == 0) {
                homeSearch.setVisibility(View.VISIBLE);
                rlActionbar.setVisibility(View.VISIBLE);
                ShareUtils.putInt(ShareUtils.IS_HOME, 0);
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
